#ifndef __TERRAINBLOCK_H__
#define __TERRAINBLOCK_H__

#include <standard/reference.h>
#include <standard/raycaster.h>
#include <standard/texturebuffer.h>
#include <standard/filewriter.h>
#include <standard/xml.h>
#include <bigimage/renderelement.h>

#define SUBBLOCKGRIDCOUNT (9)   //Horizontal and Vertical  point count (sum 81)

//8*8 Grid / 9*9 point(height map value)
#define SUBBLOCKLAYERSIZE (4)
struct STerrainBlockTextureLayerInfo{
    CTexture* m_vTerrainBlockLayer[SUBBLOCKLAYERSIZE];
    int32 m_vUVType[SUBBLOCKLAYERSIZE];
    color32 m_vTerrainBlockLayerAlphaData[SUBBLOCKGRIDCOUNT * SUBBLOCKGRIDCOUNT];
};

class CTerrainTextureList;
class CTerrainBlock: public CRefObject{
public:
    CTerrainBlock( int32 nID, int32 nBlockCountX, int32 nBlockCountY, int32 nCoordX, int32 nCoordY, float fPosX, float fPosY, float fBlockSize, CTexture* pTexture );
    virtual ~CTerrainBlock();
    void Init( float* pHeightMapData, color32 *pColorMapData, CRenderElement* pRenderElement, class CIndexBuffer* pIndexBuff );
    bool32 Open( float* pHeightMapData, color32 *pColorMapData, CRenderElement* pRenderElement, class CIndexBuffer* pIndexBuff, CTerrainTextureList* pTerrainTextureList, CXML* pXML, HXMLNODEHANDLE hBlockNode );
    bool32 Save( CFileWriter* pFileWrite );
	virtual void Update( Vector3 pCameraPos, Vector3 pCameraTarget, Quaternion pCameraQuat, int32 nDelta, float fDelta, void* pUserData );
    bool32 Intersect( CRaycaster* pRayCaster, Matrix4 pWorldMatrix, uint16* pIndexData, int32 nIndexCount, Vector3 pRet );
    bool32 UpdateGeometry();
    bool32 UpdateTextureInfo();
    void SetTextureUVScaleInfo( int32 nLayer, int32 nValue );
    void SetTextureLayerCoords( int32 x, int32 y, CTexture* pTexture, int32 nAlpha );
    void DeleteLayer( int32 nLayer );
    void ResetLayer( CTexture* pvTextureList[4], int32 vUVScale[4], color32 pLayerAlphaData[SUBBLOCKGRIDCOUNT * SUBBLOCKGRIDCOUNT] );

public:
    int32 m_nID;
    int32 m_nBlockCountX, m_nBlockCountY;
    int32 m_nCoordX, m_nCoordY;
    float m_fPosX, m_fPosY;
    float m_fBlockSize;

    Ref< CVertexBuffer* > m_rVertexBuffer;
    TV3C1T2 m_vVertex[SUBBLOCKGRIDCOUNT * SUBBLOCKGRIDCOUNT];
    float* m_pBlockHeightMapData;
    color32 *m_pBlockColorMapData;
    struct{
        STerrainBlockTextureLayerInfo m_sLayerInfo;
        Ref< CTextureBuffer* > m_rAlphaTexture;
    }m_sLayerInfo;
    CTerrainType1BatchData* m_pRenderBatchData;
};



#endif
